package com.nl.controller.web.service;

import com.alibaba.fastjson.JSONArray;

import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.interceptor.SysContext;
import com.nl.controller.common.service.CommonService;

import com.nl.controller.web.bean.SmsSendCompare;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.mapper.SmsSendCompareMapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.*;

@Slf4j
@Service
public class SmsSendCompareService {
    @Autowired
    private SmsSendCompareMapper smsSendCompareMapper;

    @Autowired
    private CommonService commonService;

      @SystemServiceLog(value = false, description = "数据源获取")
    public List<SmsSendCompare> getDateList(String flag, String cityId, String countyId, String mgrId, String mgrName) {
        List<SmsSendCompare> list = new ArrayList<>();
        HttpSession session = SysContext.getRequest().getSession();
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        SmsSendCompare smsSendCompare1 = new SmsSendCompare();
        int nowNumberAll =0;
        int lastMonthNumberAll =0;
        int lastYearMonthNumberAll =0;
        //按地市
        if ("city".equals(flag)) {
            List<SmsSendCompare> cityList = new ArrayList();
            try {
                cityList = smsSendCompareMapper.findCityList(cityId);
                log.info("获取地市列表");
            } catch (Exception e) {
                e.printStackTrace();
                log.info("获取地市列表异常");
            }
            for (SmsSendCompare a : cityList) {
                SmsSendCompare smsSendCompare = null;
                try {
                    smsSendCompare = new SmsSendCompare();
                    smsSendCompare.setCityId(a.getCityId());
                    smsSendCompare.setNowNumber(isNull(smsSendCompareMapper.findDate(this.setParam(this.getMonth(0), a.getCityId()))));
                    smsSendCompare.setLastMonthNumber(isNull(smsSendCompareMapper.findDate(this.setParam(this.getMonth(1), a.getCityId()))));
                    smsSendCompare.setLastYearMonthNumber(isNull(smsSendCompareMapper.findDate(this.setParam(this.getMonth(2), a.getCityId()))));
                    String nowNumber = smsSendCompare.getNowNumber();
                    if(nowNumber==null)
                    {
                    	nowNumber ="0";                        
                    }
                    nowNumberAll = nowNumberAll +Integer.parseInt(nowNumber);
                    String lastMonthNumber = smsSendCompare.getLastMonthNumber();
                    if(lastMonthNumber==null)
                    {
                    	lastMonthNumber ="0";                        
                    }
                    lastMonthNumberAll = lastMonthNumberAll +Integer.parseInt(lastMonthNumber);
                    String lastYearMonthNumber = smsSendCompare.getLastYearMonthNumber(); 
                    if(lastYearMonthNumber==null)
                    {
                    	lastYearMonthNumber ="0";                        
                    }
                    lastYearMonthNumberAll = lastYearMonthNumberAll +Integer.parseInt(lastYearMonthNumber);                    
                    log.info("按地市依次获取数据源" + a.getCityId());
                } catch (Exception e) {
                    e.printStackTrace();
                    log.info("按地市依次获取数据源异常" + a.getCityId());
                }                
                list.add(smsSendCompare);
            }
            //smsSendCompare1.setCityName("合计");
            smsSendCompare1.setNowNumber(String.valueOf(nowNumberAll));
            smsSendCompare1.setLastMonthNumber(String.valueOf(lastMonthNumberAll));
            smsSendCompare1.setLastYearMonthNumber(String.valueOf(lastYearMonthNumberAll));
            list.add(smsSendCompare1);

            //按项目
        } else if ("mgr".equals(flag)) {
            List<SmsSendCompare> mgrList = new ArrayList();
            Map<String, Object> mgrMap = new HashMap();
            mgrMap.put("cityId",cityId);
            mgrMap.put("countyId",countyId);
            mgrMap.put("mgrId",mgrId);
            mgrMap.put("mgrName",mgrName);
            try {
                mgrList = smsSendCompareMapper.findMgrList(mgrMap);
                log.info("获取项目列表");

            } catch (Exception e) {
                e.printStackTrace();
                log.info("获取项目列表异常");
            }
            for (SmsSendCompare a : mgrList) {
                SmsSendCompare smsSendCompare = null;
                try {
                    smsSendCompare = new SmsSendCompare();
                    smsSendCompare.setCityId(a.getCityId());
                    smsSendCompare.setMgrId(a.getMgrId());
                    smsSendCompare.setCountyId(a.getCountyId());
                    smsSendCompare.setCountyName(a.getCountyName());
                    smsSendCompare.setMgrName(a.getMgrName());
                    smsSendCompare.setNowNumber(isNull(smsSendCompareMapper.findDate(this.setParam(this.getMonth(0), a.getCityId(), a.getMgrId(),a.getCountyId(),a.getMgrName()))));
                    smsSendCompare.setLastMonthNumber(isNull(smsSendCompareMapper.findDate(this.setParam(this.getMonth(1), a.getCityId(), a.getMgrId(),a.getCountyId(),a.getMgrName()))));
                    smsSendCompare.setLastYearMonthNumber(isNull(smsSendCompareMapper.findDate(this.setParam(this.getMonth(2), a.getCityId(), a.getMgrId(),a.getCountyId(),a.getMgrName()))));
                    String nowNumber = smsSendCompare.getNowNumber();
                    if(nowNumber==null)
                    {
                    	nowNumber ="0";                        
                    }
                    nowNumberAll = nowNumberAll +Integer.parseInt(nowNumber);
                    String lastMonthNumber = smsSendCompare.getLastMonthNumber();
                    if(lastMonthNumber==null)
                    {
                    	lastMonthNumber ="0";                        
                    }
                    lastMonthNumberAll = lastMonthNumberAll +Integer.parseInt(lastMonthNumber);
                    String lastYearMonthNumber = smsSendCompare.getLastYearMonthNumber(); 
                    if(lastYearMonthNumber==null)
                    {
                    	lastYearMonthNumber ="0";                        
                    }
                    lastYearMonthNumberAll = lastYearMonthNumberAll +Integer.parseInt(lastYearMonthNumber); 
                    log.info("按项目依次获取数据源" + a.getMgrId());
                } catch (Exception e) {
                    e.printStackTrace();
                    log.info("按项目依次获取数据源异常" + a.getMgrId());
                }
                list.add(smsSendCompare);
            }
            //smsSendCompare1.setCityName("合计");
            smsSendCompare1.setNowNumber(String.valueOf(nowNumberAll));
            smsSendCompare1.setLastMonthNumber(String.valueOf(lastMonthNumberAll));
            smsSendCompare1.setLastYearMonthNumber(String.valueOf(lastYearMonthNumberAll));
            list.add(smsSendCompare1);
          //按区县
        }else if ("county".equals(flag)) {
            List<SmsSendCompare> countyList = new ArrayList();
            Map<String, Object> countyMap = new HashMap();
            countyMap.put("cityId",cityId);
            countyMap.put("countyId",countyId);
            try {
            	countyList = smsSendCompareMapper.findCountyList(countyMap);
                log.info("获取区县列表");

            } catch (Exception e) {
                e.printStackTrace();
                log.info("获取区县列表异常");
            }
            for (SmsSendCompare a : countyList) {
                SmsSendCompare smsSendCompare = null;
                try {
                    smsSendCompare = new SmsSendCompare();
                    smsSendCompare.setCityId(a.getCityId());
                    smsSendCompare.setCountyId(a.getCountyId());
                    smsSendCompare.setCountyName(a.getCountyName());
                    smsSendCompare.setNowNumber(isNull(smsSendCompareMapper.findDate(this.setParam(this.getMonth(0), a.getCityId(), a.getCountyId()))));
                    smsSendCompare.setLastMonthNumber(isNull(smsSendCompareMapper.findDate(this.setParam(this.getMonth(1), a.getCityId(), a.getCountyId()))));
                    smsSendCompare.setLastYearMonthNumber(isNull(smsSendCompareMapper.findDate(this.setParam(this.getMonth(2), a.getCityId(), a.getCountyId()))));
                    String nowNumber = smsSendCompare.getNowNumber();
                    if(nowNumber==null)
                    {
                    	nowNumber ="0";                        
                    }
                    nowNumberAll = nowNumberAll +Integer.parseInt(nowNumber);
                    String lastMonthNumber = smsSendCompare.getLastMonthNumber();
                    if(lastMonthNumber==null)
                    {
                    	lastMonthNumber ="0";                        
                    }
                    lastMonthNumberAll = lastMonthNumberAll +Integer.parseInt(lastMonthNumber);
                    String lastYearMonthNumber = smsSendCompare.getLastYearMonthNumber(); 
                    if(lastYearMonthNumber==null)
                    {
                    	lastYearMonthNumber ="0";                        
                    }
                    lastYearMonthNumberAll = lastYearMonthNumberAll +Integer.parseInt(lastYearMonthNumber);
                    log.info("按区县依次获取数据源" + a.getCountyId());
                } catch (Exception e) {
                    e.printStackTrace();
                    log.info("按区县依次获取数据源异常" + a.getCountyId());
                }
                list.add(smsSendCompare);
            }
            //smsSendCompare1.setCityName("合计");
            smsSendCompare1.setNowNumber(String.valueOf(nowNumberAll));
            smsSendCompare1.setLastMonthNumber(String.valueOf(lastMonthNumberAll));
            smsSendCompare1.setLastYearMonthNumber(String.valueOf(lastYearMonthNumberAll));
            list.add(smsSendCompare1);
        }
        return list;
    }

    @SystemServiceLog(description = "同比环比", value = false)
    public PageInfo<SmsSendCompare> findSmsSendCompare(int page, int limit, String flag, String cityId, String countyId, String mgrId, String mgrName) {
        PageInfo<SmsSendCompare> pageInfo;
        try {
            List<SmsSendCompare> list = this.getDateList(flag, cityId, countyId, mgrId, mgrName);
            List tempList = new ArrayList();//临时的分页数据
            for (int i = 0; i < limit; i++) {
                int seq = (page - 1) * 10 + i;
                if (seq < list.size())
                    tempList.add(list.get(seq));
            }
            pageInfo = new PageInfo<>(tempList);
            pageInfo.setTotal((long) list.size());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("同比环比查询出现异常：" + flag);
            throw new SmsException("同比环比查询出现异常：" + flag);
        }
        return pageInfo;
    }


    @SystemServiceLog(description = "导出", value = false)
    public JSONArray download
            (String flag, String cityId, String countyId, String mgrId, String mgrName) {
        {
            List<SmsSendCompare> list = new ArrayList<>();
            try {
                list = this.getDateList(flag,cityId,countyId,mgrId,mgrName);
                if(!"".equals(mgrId)&&null!=mgrId){
                	commonService.createSmsOperLog("7", mgrId,"发送量同比环比表导出："+mgrId, "0", "29");
                }else{
                	commonService.createSmsOperLog("7", "0","发送量同比环比表导出", "0", "29");
                }
            } catch (Exception e) {
            	if(!"".equals(mgrId)&&null!=mgrId){
                	commonService.createSmsOperLog("7", mgrId,"发送量同比环比表导出："+mgrId, "1", "29");
                }else{
                	commonService.createSmsOperLog("7", "0","发送量同比环比表导出", "1", "29");
                }
                throw new SmsException("同比环比导出失败");
            }
            JSONArray jsonArray = JSONArray.parseArray(JSONArray.toJSONString(list));
            return jsonArray;
        }
    }
    
    
    /**
     * 查询信息设置
     *
     * @param month
     * @param countyId
     * @return
     */
    private Map setParam(String month, String cityId,String countyId) {
        Map map = new HashMap();
        map.put("month", month);
        map.put("cityId", cityId);
        map.put("countyId", countyId);
        return map;
    }

    /**
     * 查询信息设置
     *
     * @param month
     * @param mgrId
     * @return
     */
    private Map setParam(String month, String cityId, String mgrId, String countyId, String mgrName) {
        Map map = new HashMap();
        map.put("month", month);
        map.put("cityId", cityId);
        map.put("mgrId", mgrId);
        map.put("countyId", countyId);
        map.put("mgrName", mgrName);
        return map;
    }

    /**
     * 查询信息设置
     *
     * @param month
     * @param cityId
     * @return
     */
    private Map setParam(String month, String cityId) {
        Map map = new HashMap();
        map.put("month", month);
        map.put("cityId", cityId);
        return map;
    }

    /**
     * 月份获取
     *
     * @param flag 0 当月  1 上月    2 去年当月
     * @return
     */
    private String getMonth(int flag) {
        Calendar cal = Calendar.getInstance();
        StringBuffer sbf = new StringBuffer();
        int year = cal.get(Calendar.YEAR);  //当前年份
        int month = (cal.get(Calendar.MONTH));//当前月份
        //当月
        if (flag == 1) {
            month = month - 1;
            //去年当个月
        } else if (flag == 2) {
            year = year - 1;
        }
        year = month > 0 ? year : year - 1;
        month = month > 0 ? month : month + 12;
        sbf = sbf.append(year);
        sbf = sbf.append(month > 9 ? String.valueOf(month) : ("0" + String.valueOf(month)));
        return sbf.toString();
    }

    /**
     * 数据为0的处理
     *
     * @param parm
     * @return
     */
    private String isNull(String parm) {
        if ("".equals(parm) || parm == null) {
            return "0";
        } else {
            return parm;
        }

    }
}